import java.util
        .concurrent.ExecutorService;


import java.util
        .concurrent.Executors;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author shilinghuai
 * @create 2022/4/4
 * @since 1.0.0
 */
public class ThreePrint {
    public static String wakeFlag = "A";

    public static void main(String[] args){

        ExecutorService executorService = Executors.newFixedThreadPool(3);
        Object o = new Object();
        executorService.execute(new MyThread("A",o));
        executorService.execute(new MyThread("B",o));
        executorService.execute(new MyThread("C",o));
    }
    static class MyThread extends Thread{
        private Object lock;
        private String workFlag;
        private int countA = 1;
        private int countB = 1;
        private int countC = 1;
        public MyThread(){

        }
        public MyThread(String key,Object lock){
            this.workFlag = key;
            this.lock = lock;
        }
        public void run(){

            if(workFlag.equals("A")){
                while (true){
                    //先竞争锁在计数
                    synchronized (lock){
                        while (!wakeFlag.equals("A")){

                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        System.out.println("A");
                        wakeFlag="B";
                        lock.notifyAll();
                        if(countA++>=10){
                            break;
                        }
                    }
                }

            }else if(workFlag.equals("B")){
                //先竞争锁在计数
                while (true){
                    synchronized (lock){
                        while (!wakeFlag.equals("B")){

                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        System.out.println("B");
                        wakeFlag="C";
                        lock.notifyAll();
                        if(countB++>=10){
                            break;
                        }
                    }
                }

            }else if(workFlag.equals("C")){
                //先竞争锁在计数
                while (true){
                    synchronized (lock){
                        while (!wakeFlag.equals("C")){

                            try {
                                lock.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        System.out.println("C");
                        wakeFlag = "A";
                        lock.notifyAll();
                        if(countC++>=10){
                            break;
                        }
                    }
                }

            }
        }
    }
}
